package br.fbv.compreja.activity.carrinho;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.fbv.compreja.R;
import br.fbv.compreja.activity.produto.ProdutoActivity;
import br.fbv.compreja.negocio.compra.CompraController;
import br.fbv.compreja.negocio.compra.ItemCompra;
import br.fbv.compreja.negocio.produto.Produto;

public class CarrinhoListAdapter extends BaseAdapter {
	private List<ItemCompra> compras;
	private LayoutInflater mInflater;
	private Activity activity;

	public CarrinhoListAdapter(Activity activity) {
		this.activity = activity;
		compras = CompraController.getInstance(activity)
				.consultarComprasEmAberto();
		mInflater = (LayoutInflater) this.activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return compras.size();
	}

	@Override
	public Object getItem(int arg0) {
		return compras.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item1, null);
			holder = new ViewHolder();
			holder.textView = (TextView) convertView.findViewById(R.id.text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ItemCompra itemCompra = compras.get(position);
		final Produto produto = itemCompra.getProduto();
		holder.textView.setText(produto.getNome());
		convertView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(activity, ProdutoActivity.class);
				intent.putExtra(Produto.BARCODE, produto.getBarcode());
				intent.putExtra(ItemCompra.QUANTIDADE,
						itemCompra.getQuantidade());
				activity.startActivity(intent);
			}
		});
		return convertView;
	}

	public static class ViewHolder {
		public TextView textView;
	}

}
